<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="net.sf.json.JSONObject"%>
<%@page import="com.sinodata.bsm.center.util.spring.SpringContextHolder"%>
<%@page import="com.sinodata.bsm.center.service.config.PortalService"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@page import="com.sinodata.bsm.center.user.UserUtil"%>
<%@page import="com.sinodata.bsm.center.purview.UserFunctionValidate"%>
<%@page import="com.sinodata.bsm.center.purview.FunctionConstants"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/charts/FusionCharts.js"></script>
<%
String param = request.getParameter("args");
JSONObject parameter = "".equals(param) ? null : JSONObject.fromObject(param);
Integer time = null;
if(parameter == null){
    time = 0;
}else{
    time = Integer.valueOf(parameter.getString("PARAM_time"));
}
String format = "yyyyMMdd";
SimpleDateFormat simple = new SimpleDateFormat(format);
StringBuffer sbTime = new StringBuffer();
if(time == 1){//今天
    sbTime.append("and  occur_time").append(" > to_date('" + simple.format(new Date()) + "','yyyy-MM-dd hh24:mi')");
}else if(time == 2){//本周
    Calendar calendar = Calendar.getInstance(Locale.CHINA);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    sbTime.append("and  occur_time").append(" > to_date('" + simple.format(calendar.getTime()) + "','yyyy-MM-dd hh24:mi')");
}else if(time == 3){//本月
    Calendar calendar1 = Calendar.getInstance(Locale.CHINA);
    calendar1.add(Calendar.MONTH, -1);
    sbTime.append("and  occur_time").append(" > to_date('" + simple.format(calendar1.getTime()).substring(0, 6) + "01','yyyy-MM-dd hh24:mi')");
}
StringBuffer sql = new StringBuffer();
sql.append("select sum(case when  level_ = 2 then 1 else 0 end) as l2,");
sql.append("sum(case when  level_ = 3 then 1 else 0 end) as l3,");
sql.append("sum(case when  level_ = 4 then 1 else 0 end) as l4,");
sql.append("sum(case when  level_ = 5 then 1 else 0 end) as l5");
sql.append(" from ( select e.id,e.level_ from event e");
List<Long> authScopeList = UserFunctionValidate.getResIdListForUser(FunctionConstants.VIEW_RES);
if(!UserUtil.getInstance().isSysAdmin(UserUtil.getInstance().getCurrentUserId()) && authScopeList != null && authScopeList.size()!=0){
    sql.append(" where e.res_id").append(" in (");
    StringBuffer str = new StringBuffer();
    for (Long resId : authScopeList) {
        str.append(resId).append(",");
    }
    str.delete(str.lastIndexOf(","), str.length());
    sql.append(str);
    sql.append(") ");
    sql.append(sbTime);
    sql.append(" UNION ");
    sql.append(" select e.id,e.level_ from event e where e.res_id in (");
    sql.append(" select end_id  from res_relation start with start_id in ( ");
    sql.append(str);
    sql.append(") and relation_type_id  in ( select  id from relation_type  start with id = 10 connect by prior  id = parent ) connect by prior  end_id = start_id");
    sql.append(" ) ");
    sql.append(sbTime);
}else{
    sql.append(" where 1=1 ");
    sql.append(sbTime);
}
sql.append(")");
PortalService service = SpringContextHolder.getBean(PortalService.class);
List<Map<String,Object>> list = service.findListBySql(sql.toString());
%>
<script type="text/javascript">
$(document).ready(function(){
		 var myChart = new FusionCharts( "${pageContext.request.contextPath}/resources/charts/Column2D.swf", "myChartId", "99%", "200", "0", "1" ); 
		 myChart.setJSONData(
				 {
					  "chart": {
					    "formatnumberscale": "0",
					    "decimals": "0",
					    "showvalues": "0",
					    "yaxisname": "事件数",
					    "palette": "2"
					  },
					  "data": [
					    {
					      "value": "<%=list.get(0).get("L2")%>",
					      "label": "轻微",
					      "color": "#0c7ce5"
					    },
					    {
					      "value": "<%=list.get(0).get("L3")%>",
					      "label": "警告",
					      "color": "#dede03"
					    },
					    {
					      "value": "<%=list.get(0).get("L4")%>",
					      "label": "重要",
					      "color": "#ff9937"
					    },
					    {
					      "value": "<%=list.get(0).get("L5")%>",
					      "label": "严重",
					      "color": "#ce110f"
					    }
					  ],
					  "styles": {
					    "definition": [
					      {
					        "duration": "1",
					        "start": "0",
					        "param": "_yScale",
					        "type": "animation",
					        "name": "myAnim"
					      }
					    ],
					    "application": [
					      {
					        "styles": "myAnim",
					        "toobject": "VLINES"
					      }
					    ]
					  }
					}	 
		 );      
		
		 myChart.render("chartContainer");    
});
	</script> 
	<div id="chartContainer"></div>
